package com.cs.beneficios.web.action;

import com.cs.beneficios.business.model.RepAtributoVO;
import com.cs.beneficios.common.ConnectionFactory;

import java.io.IOException;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class GenerarSelectEGAction extends Action {
    /**This is the main action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
                                 HttpServletRequest request, 
                                 HttpServletResponse response) throws IOException, 
                                                                      ServletException {
        HttpSession session = request.getSession();
        String subpro = (String)session.getAttribute("generar");
        String sede = (String)session.getAttribute("sede");
        
        //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
        List lFiltros = new LinkedList();
        List lBeneficiario = new LinkedList();
        List lAtributos = new LinkedList();
        
        List lEspecificos = new LinkedList();
        List lGenerales = new LinkedList();
        
        lFiltros = (List)session.getAttribute("ListaFiltrado");
        session.setAttribute("listafiltros",lFiltros);
        lBeneficiario = (List)session.getAttribute("listabeneficiario");
        lAtributos = (List)session.getAttribute("listaatributos");
        
        //  if((Object)lFiltros != null)
        // {
          if((Object)lBeneficiario != null)
          {
             int tamano1 = lBeneficiario.size();
             for(int i=0;i<tamano1;i++)
             {
               RepAtributoVO repatriVO = (RepAtributoVO)lBeneficiario.get(i);
               String valor1 ="atrbeneficiario"+i;
               String valor2 = request.getParameter(valor1);
               if(valor2 != null)
               {
                 RepAtributoVO repespecVO = new RepAtributoVO();
                 repespecVO.setNombre(repatriVO.getNombre());
                 repespecVO.setEtiqueta(repatriVO.getEtiqueta());
                 lGenerales.add(repespecVO);
               }
             }
             session.setAttribute("listageneral",lGenerales);
             if((Object)lAtributos != null)
             {
               int tamano = lAtributos.size();
               for(int i=0;i<tamano;i++)
               {
                 RepAtributoVO repatriVO = (RepAtributoVO)lAtributos.get(i);
                 //Long valor1 =(repatriVO.getCodigo());
                 String valor = "atributo"+i;
                 String valor2 = request.getParameter(valor);
                 if(valor2 != null)
                 {
                   RepAtributoVO repespecVO = new RepAtributoVO();
                   repespecVO.setCodigo(repatriVO.getCodigo());
                   repespecVO.setEtiqueta(repatriVO.getEtiqueta());
                   lEspecificos.add(repespecVO);
                 }
               }
               session.setAttribute("listaEspecificos",lEspecificos);
             }else
             {
               session.setAttribute("msg","Seleccione alguna opcion en la lista de beneficios");
             }
          }else
          {
            session.setAttribute("msg","Seleccione alguna opcion en la lista del beneficiario");
          }
        // }else
        // {
        //   session.setAttribute("msg","Seleccione algun filtro para realizar la consulta");
        // }
        
        
        return mapping.findForward("success");
        }
}
